package com.atbun.eduservice.entity.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atbun.eduservice.entity.EduSubject;
import com.atbun.eduservice.service.EduSubjectService;
import com.atbun.servicebase.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Map;

/**
 * @Description :读取Excel监听器
 * 该类不可以使用spring自动注入，需要自定义注入
 * @Author : mayp
 * @Date: 2020-05-05 15:59
 */
public class SubjectExcelListener extends AnalysisEventListener<ExcelSubjectData> {

//    由于不能交给spring管理，那么就需要自定义属性
    public EduSubjectService subjectService;

    public SubjectExcelListener() {}

    //创建有参数构造，传递subjectService用于操作数据库
    public SubjectExcelListener(EduSubjectService subjectService) {
        this.subjectService = subjectService;
    }

    /**
     * 一行一行去读取Excel内容
     * @param excelSubjectData
     * @param analysisContext
     */
    @Override
    public void invoke(ExcelSubjectData excelSubjectData, AnalysisContext analysisContext) {
        if (null == excelSubjectData){
            throw new GuliException(20001,"添加失败");
        }
//        添加一级分类
        EduSubject existOneSubject = this.existOneSubject(subjectService,excelSubjectData.getOneSubjectName());
        if (null == existOneSubject){//没有相同的
            existOneSubject = new EduSubject();
            existOneSubject.setTitle(excelSubjectData.getOneSubjectName());
            existOneSubject.setParentId("0");
            subjectService.save(existOneSubject);
        }

//        获取一级分类id值
        String pid = existOneSubject.getId();

//        添加二级分类
        EduSubject exisTowSubject = this.exisTowSubject(subjectService,excelSubjectData.getTwoSubjectName(),pid);
        if (null == exisTowSubject){
            exisTowSubject = new EduSubject();
            exisTowSubject.setTitle(excelSubjectData.getTwoSubjectName());
            exisTowSubject.setParentId(pid);
            subjectService.save(exisTowSubject);
        }

    }

//    读取表头信息
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.printf("表头信息："+headMap);
    }

    //    完成后执行
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    //判断一级分类是否重复
    private EduSubject existOneSubject(EduSubjectService subjectService,String name){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",0); //一级分类默认为0
        EduSubject eduSubject = subjectService.getOne(wrapper);
        return eduSubject;
    }

//    判断二级分类是否重复
    public EduSubject exisTowSubject(EduSubjectService subjectService,String nam,String pid){
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",nam);
        wrapper.eq("parent_id",pid);
        EduSubject eduSubject = subjectService.getOne(wrapper);
        return eduSubject;
    }

}
